package com.school.admin.common.imit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.school.admin.common.result.BaseResult;
import com.school.admin.common.utils.Db;
import com.school.admin.common.utils.RedisUtil;
import com.school.admin.entity.DishesDict;
import com.school.admin.entity.vo.PurviewVo;
import com.school.admin.mapper.DishesDictMapper;
import com.school.admin.mapper.MallSysPurviewMapper;
import com.school.admin.service.MallMenuService;
import com.school.admin.service.MallSysPurviewService;
import com.school.admin.service.MallSysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ：sunpx
 * @date ：Created in 2021/8/8 18:47
 * @description：初始化字典
 * @modified By：
 * @version: $
 */
@Component
@Slf4j
public class InitDict {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    MallSysRoleService mallSysRoleService;
    @Autowired
    MallMenuService mallMenuService;

    @Autowired
    MallSysPurviewService mallsyspurviewservice;

    @Autowired
    ApplicationContext applicationContext;





    @Bean
    public void parameterDict() {
        log.info("开始初始化字典");
        extracted();
        log.info("初始化字典成功");







        log.info("查询接口所需角色开始");
        initPermission();
        log.info("查询接口所需角色结束");

 /*       RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        List<MallSysPurview> list = new ArrayList<>();
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods();
        handlerMethods.forEach((key, value) ->{
            MallSysPurview mallSysPurview = new MallSysPurview();

            String purview_url = value.toString();
            String address =key.getPatternsCondition().getPatterns().toArray()[0].toString();
            Set<RequestMethod> methods = key.getMethodsCondition().getMethods();
            mallSysPurview.setPurviewUrl(purview_url).setAddress(address);

            if (!CollectionUtils.isEmpty(methods)) {
                String method = methods.toArray()[0].toString();
                mallSysPurview.setMethod(method);
            }
            list.add(mallSysPurview);
//            String method = key.getMethodsCondition().getMethods().toArray()[0].toString();
        });
        mallsyspurviewservice.saveBatch(list);*/

    }

    public void extracted() {
        DishesDictMapper mapper = Db.getMapper(DishesDictMapper.class);
        QueryWrapper<DishesDict> dishesDictQueryWrapper = new QueryWrapper<>();
        List<DishesDict> dishesDictList = mapper.selectList(dishesDictQueryWrapper);
        sealAnOnRedis(dishesDictList);
    }


    public void sealAnOnRedis(List<DishesDict> dishesDictList) {

        for (DishesDict dishesDict : dishesDictList) {
            //系统参数
            if (BaseResult.SysTypeMapping.SYS_PARAMETER.getType().equals(dishesDict.getTypeCode())) {
                redisUtil.hset(BaseResult.SysTypeMapping.SYS_PARAMETER.getType(), dishesDict.getDictKey(), dishesDict.getValue());
                log.info(dishesDict.getRemark() + "path初始化完成");
                continue;
            }

            if (BaseResult.SysTypeMapping.SELECT_TYPE_API.getType().equals(dishesDict.getTypeCode())) {
                redisUtil.hset(BaseResult.SysTypeMapping.SELECT_TYPE_API.getType(), dishesDict.getDictKey(), dishesDict.getValue());
                log.info(dishesDict.getRemark() + "初始化完成");
            }

        }

    }


    public void initPermission(){
        MallSysPurviewMapper mapper = Db.getMapper(MallSysPurviewMapper.class);
        List<PurviewVo> allPurviewRoles = mapper.getAllPurviewRoles();
        redisUtil.delete(BaseResult.SysTypeMapping.INTERFACE_REQUIRED_ROLE.getType());
        allPurviewRoles.forEach(res->{
            redisUtil.hset(BaseResult.SysTypeMapping.INTERFACE_REQUIRED_ROLE.getType(), String.valueOf(res.getAddress()), res.getRoleNames());
        });
    }



}
